import { _decorator, Component, director, EventTouch, Node, v2, v3, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

const v2_1 = v2(0);
const v3_1 = v3(0);
const v3_2 = v3(0);

@ccclass('Rotate')
export class Rotate extends Component {
    @property(Node)
    cameraNode!: Node;

    onEnable() {
        this.node.on(Node.EventType.TOUCH_MOVE, this._onTouchMove, this)
    }
    onDisable() {
        this.node.off(Node.EventType.TOUCH_MOVE, this._onTouchMove, this)
    }

    private _onTouchMove(event: EventTouch) {
        let touches = event.getTouches();
        if (touches.length == 1) {
            let rotateDelta = event.getDelta(v2_1);
            if (rotateDelta.length() > 1) {
                this._rotateByCenter(v3_1.set(rotateDelta.y * 0.5, -rotateDelta.x));
            }
        }
    }

    private _rotateByCenter(addVec3: Vec3) {
        let vec3 = this.cameraNode.eulerAngles, mEv = v3_2.set(vec3).add(addVec3.multiplyScalar(3));
        this.cameraNode.eulerAngles = vec3.lerp(mEv, 0.1);
    }
}


